package com.example.eventreg.Student;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.eventreg.security.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.coyote.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;
@RequestMapping("/student")
@RestController // 用于定义该类为一个RESTful API控制器
public class StudentController {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudentService studentService;
    @Autowired
    private JwtUtil jwtUtil;

    // 构造函数，用于初始化模拟数据
    public StudentController() {
        // 初始化一些用户数据
    }
    @GetMapping("/self")
    public Student getStudentSelf(@RequestHeader("Authorization") String token) {
        System.out.println("token: "+token);
        if (token != null) {
            // 去除 "Bearer " 前缀，获取纯净的 token
            if(token.startsWith("Bearer "))
            {
                token = token.substring(7);
            }
            System.out.println("tokenC: "+token);
            if (jwtUtil.getTypeFromToken(token).equals("S"))
            {
                String sid = jwtUtil.getUserIdFromToken(token);
                return studentMapper.selectOne(new QueryWrapper<Student>().eq("sid", sid));
            }
        }
        return null;
    }
    //获取所有学生
    @GetMapping("/get")
    public List<Student> getAllUsers() {
        return studentMapper.selectList(null); // 返回200状态码和用户列表
    }
    //获取指定学生
    @GetMapping("/{sid}")
    public Student getOneStudent(@PathVariable String sid) {
        return studentMapper.selectOne(new QueryWrapper<Student>().eq("sid",sid)); // 返回200状态码和用户列表
    }
    //学校获取
    @GetMapping("/school/{value}")
    public List<Student> getOneStudentBySchool(@PathVariable String value) {
        return studentMapper.selectList(new QueryWrapper<Student>().eq("school",value)); // 返回200状态码和用户列表
    }
    //年级获取
    @GetMapping("/grade/{value}")
    public List<Student> getOneStudentByGrade(@PathVariable String value) {
        return studentMapper.selectList(new QueryWrapper<Student>().eq("grade",value)); // 返回200状态码和用户列表
    }
    //创建新学生用户
    @PostMapping("/reg")
    public Student createStudent(@RequestBody Student student) {
        studentMapper.insert(student);
        return student;
    }
    // 更新指定ID的用户
    @PutMapping("/update")
    public Integer updateStudent(@RequestBody Student updatedStudent) {
        return studentMapper.updateById(updatedStudent);
    }
    //删除指定用户
    @PutMapping("/del/{sid}")
    public Integer deleteStudent(@PathVariable String sid) {
        return studentMapper.deleteById(sid);
    }
    // 映射GET请求，获取所有用户
    /*

    // 禁用指定ID的用户
    @PutMapping("/disable/{id}")
    public String disableUser(@PathVariable Long id) {
        // 创建UpdateWrapper来设置更新条件
        UpdateWrapper<Student> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id); // 条件：ID匹配
        updateWrapper.set("status", "DISABLED"); // 更新status为DISABLED
        // 执行更新操作
        boolean updated = studentMapper.update(null, updateWrapper) > 0;
        // 返回禁用成功的消息
        if (updated) {
            return "用户ID【 " + id + " 】已禁用";
        } else {
            return "禁用用户失败，用户ID【 " + id + " 】不存在或更新失败";
        }
    }*/

}